<?php

require_once("./lib/defaults.php");
require_once("./cfg/settings.php");

global $CONSOLE_SOCKET;
$CONSOLE_SOCKET_LIST = array();
global $CONSOLE_SOCKET_LIST;

function console_listen()
{
	global $CONSOLE_SOCKET;
	
	unlink("./console.socket");

	$CONSOLE_SOCKET = stream_socket_server("unix://./console.socket", $errno, $errstr);
	if(!$CONSOLE_SOCKET)
	{
		echo "Failed to listen on console socket: $errstr\n";
	}

	chmod("./console.socket",0664);

	stream_set_blocking($CONSOLE_SOCKET, false);
}

function console_accept()
{
	echo "Accepting Connection\n";
	global $CONSOLE_SOCKET;
	global $CONSOLE_SOCKET_LIST;
	
	$sock = stream_socket_accept($CONSOLE_SOCKET);
	stream_set_blocking($sock, false);

	$CONSOLE_SOCKET_LIST[] = $sock;
	echo "Done Accepting\n";
}

function console_write($msg)
{
	global $CONSOLE_SOCKET_LIST;
	foreach($CONSOLE_SOCKET_LIST as $num => $sock)
	{
		if(!fwrite($sock, $msg))
			unset($CONSOLE_SOCKET_LIST[$num]);
	}
}

?>